leonardo_lib = "../dist/lib"
main_config = "../dist/cgi-bin/leonardo.config"

import sys; sys.path.append(leonardo_lib)

import StringIO

import config
import core
import request_response


class Tester:


    def __init__(self):
        self.leonardo = core.Leonardo(config.Configurator(main_config))


    # @@@ this should really just be a method in core.py 
    def test(self, request):
        
        resource_id = request.relative_path
        
        if request.method == "GET":
            resource = self.leonardo.resource_manager.get(resource_id, request, None)
        elif request.method == "POST":
            resource = self.leonardo.resource_manager.get(resource_id, request, None)
        else:
            resource = None
        
        output = StringIO.StringIO()
        
        response = request_response.Response(resource)
        response.send(output)
        
        return output.getvalue()



class MockFields:


    def getvalue(self, key):
        return None


class MockRequest:


    def __init__(self, method, path_info, query_string="", fields=None):
        self.fields = fields or MockFields()
        self.path_info = path_info
        self.relative_path = self.path_info.strip("/")
        self.method = method
        self.query = query_string


    def logged_in(self):
        return False        



class LoggedInMockRequest:


    def __init__(self, method, path_info, query_string="", fields=None):
        self.fields = fields or MockFields()
        self.path_info = path_info
        self.relative_path = self.path_info.strip("/")
        self.method = method
        self.query = query_string


    def logged_in(self):
        return True        
    
    
    def get_user(self):
        return "user@test"
        
        
        
tester = Tester()

# test bug: issue160
tester.test(LoggedInMockRequest("GET", "/edit"))

# test bug: issue163
tester.test(MockRequest("GET", "/category"))

# test bug: issue169
tester.test(MockRequest("GET", "/category/foo"))

# test bug: issue159
tester.test(MockRequest("GET", "/blog/2004/12/16/my_first_blog_entry/__comment__1"))